<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Db;
class Order
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::name("goods_order")->select();
		return json(["code"=>200,"data"=>$res]);
    }
	public function info(){
		$res = Db::name("goods_order")->select()->toArray();
		foreach($res as $k=>$v){
			$u = Db::name("user")->where("id",$v["userId"])->field("name")->select();
			$addr = Db::name("address")->where("id",$v["addr_id"])->select();
			if($v["cart_ids"]){
				 $cart = Db::query("select * from cart where id in (".$v["cart_ids"].")");
				 foreach($cart as $ck=>$cv){
					 $cart[$ck] = Db::query("select name as gname,sid,sname,sprice from goods as g right join (select id as sid,name as sname,goodsId,price as sprice from sku where id = ".$cv["skuId"].") as s on g.id=s.goodsId");
				 }
				 $res[$k]["info"] =$cart;
			}else{
				 $res[$k]["info"][0] = Db::query("select name as gname,sid,sname,sprice from goods as g right join (select id as sid,name as sname,goodsId,price as sprice from sku where id = ".$res[$k]["goods_sku_id"].") as s on g.id=s.goodsId");
			}
			$res[$k]["u"] = $u;
			$res[$k]["addr"] = $addr;
		}
		return json(["code"=>200,"data"=>$res]);
	}
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$d = array_merge($p,["cart_ids"=>"","order_coupon_id"=>0,"order_integral"=>0,"order_use_integral"=>0,"createTime"=>$date,"updateTime"=>$date]);
		Db::name("goods_order")->save($d);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
	public function updateStateById(){
		$p = input("get.");
		Db::query("update goods_order set state = ? where id = ?",[$p["state"],$p["id"]]);
		return json(["code"=>200,"msg"=>"操作成功"]);
	}
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$id = input("get.id");
		Db::name("goods_order")->where("id",$id)->delete();
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
